



//
//  MainViewController.m
//  框架
//
//  Created by MyMac on 15/9/23.
//  Copyright (c) 2015年 tongfang. All rights reserved.
//

#import "MainViewController.h"
#import "AViewController.h"
#import "BViewController.h"
#import "CViewController.h"
#import "DViewController.h"
#import "UINavigationItem+NH.h"
@interface MainViewController ()
@property(nonatomic,strong)UIViewController*currentVC;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor=[UIColor redColor];
    
  
    AViewController*Avc=[[AViewController alloc]init];
  Avc.view.frame=CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-49);
[UINavigationItem copyFrom:self.navigationItem to:Avc.navigationItem];
    [self addChildViewController:Avc];
    
   
    
    
    
    BViewController*Bvc=[[BViewController alloc]init];
   
    [self addChildViewController:Bvc];
     Bvc.view.frame=CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-49);
    
    
    
    CViewController*Cvc=[[CViewController alloc]init];

    [self addChildViewController:Cvc];
        Cvc.view.frame=CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-49);
    
   
    
    
    DViewController*Dvc=[[DViewController alloc]init];
   
    [self addChildViewController:Dvc];
     Dvc.view.frame=CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-49);
  
    
    
    self.currentVC=Avc;
    
    [self createTabBar];
    [self.view addSubview:Avc.view];
   
    
}
-(void)createTabBar
{
    NSArray*titleArray=@[@"A",@"B",@"C",@"D"];
    UIView*barView=[[UIView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height-49, self.view.bounds.size.width, 49)];

    [self.view addSubview:barView];
    for (NSInteger i=0; i<titleArray.count; i++) {
        CGFloat w=barView.bounds.size.width/titleArray.count;
        CGFloat h=barView.bounds.size.height;
        CGFloat y=0;
        CGFloat x=i*w;
        UIButton*item=[[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
        item.backgroundColor=[UIColor yellowColor];
        [item setTitle:titleArray[i] forState:UIControlStateNormal];
        [item setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        item.tag=i;
        [item addTarget:self action:@selector(itemSelector:) forControlEvents:UIControlEventTouchUpInside];
        [barView addSubview:item];
        
    }

}
-(void)itemSelector:(UIButton*)btn
{
    UIViewController*fromvc=self.currentVC;
    UIViewController*Tovc=self.childViewControllers[btn.tag];
    if (fromvc==Tovc) {
        return;
    }
    
 
   
  __weak  typeof(self) vc=self;
  [self transitionFromViewController:fromvc toViewController:Tovc duration:1.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        NSLog(@"hahaha");
    } completion:^(BOOL finished) {
        NSLog(@"finish");
        vc.currentVC=Tovc;
    }];

    [UINavigationItem copyFrom:self.navigationItem to:Tovc.navigationItem];




}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc
{
    NSLog(@"%@",self);

}


@end
